package cn.tedu.egaddress0805.controler;

import cn.tedu.egaddress0805.mapper.AddressMapper;
import cn.tedu.egaddress0805.pojo.dto.AddressAddParam;
import cn.tedu.egaddress0805.pojo.entity.Address;
import cn.tedu.egaddress0805.pojo.vo.AddressInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/address/")
public class AddressControler {
   @Autowired
    private AddressMapper addressMapper;
   @PostMapping("add")
    public String addAddress(@RequestBody AddressAddParam addressAddParam){
       System.out.println("添加地址："+addressAddParam);
       Address address = new Address();
       BeanUtils.copyProperties(addressAddParam,address);
       System.out.println("添加地址:"+address);
       addressMapper.insertAddress(address);
       return "添加地址成功";
   }
   @GetMapping("list")
   public List<AddressInfoVo> listAllAddress(){
       List<AddressInfoVo> list = addressMapper.listAllAddress();
       System.out.println("查询所有地址："+list);
       return addressMapper.listAllAddress();
   }
    @PostMapping ("del/{id}")
    public String deleteAddress(@PathVariable Integer id){
       System.out.println("删除地址："+id);
       int result=addressMapper.deleteAddress(id);
       if(result>0){
           return "删除成功";
       }
       return "删除失败";
    }
}
